home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_13_09
/
barbu2
/
linein.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1995-05-07
|
2KB
|
118 lines
//////// AB CLASSGEN Mon May 01 19:18:11 1995 ////////
// LINEIN Implementation
//////////////////////////////////////////////////////
#include "LINEIN.HPP"
#include <stdio.h>
#include <values.h>
#include <ctype.h>
#include <dir.h>
LINEIN::LINEIN(const char far lpszText[])
{
_commonCtorJob(lpszText);
}
LINEIN::LINEIN(HINSTANCE hInstance,
const char szFileOrResName[],
const char szResType[])
{
char far *lpText;
_s = 0;
if(0 != szResType){ // load from resource
HGLOBAL hRes = LoadResource(hInstance,
FindResource(hInstance,
szFileOrResName,
szResType
)
);
lpText = (char far*)LockResource(hRes);
_commonCtorJob(lpText);
GlobalUnlock(hRes);
FreeResource(hRes);
}
else{ // load from file
struct ffblk ff;
if(0 != findfirst(szFileOrResName, &ff, 0))
return;
long size = ff.ff_fsize;
if(size >= MAXINT)
return;
if(0 == (lpText = new char[1+size]))
return;
lpText[size] = 0;
FILE* f = fopen(szFileOrResName, "rt");
fread(lpText, 1, size, f);
fclose(f);
_commonCtorJob(lpText);
delete lpText;
}
}
void LINEIN::_commonCtorJob(const char far lp[])
{
_x = 0;
if(0 == (_s = new STR(1+strlen(lp))))
return;
int i = 0;
while(isspace(lp[i]))
i++;
while(lp[i]){
if(i > 0 && lp[i] == '\\' && lp[i-1] !='\\'
&& lp[i+1] == '\n'){
do i++; while(isspace(lp[i]));
}
*_s += lp[i++];
if(lp[i-1] == '\n'){
while(isspace(lp[i]))
i++;
}
}
}
LINEIN::~LINEIN()
{
if(0 != _s){
delete _s;
_s = 0;
}
}
void LINEIN::reset()
{
_x = 0;
}
static void Copy(const char* p, int i, int j, STR& dest)
{
STR temp(j-i+1);
for(int k = i; k < j; k++)
temp += p[k];
dest = temp;
}
const char * LINEIN::get(STR& Line)
{
if(0 == _s || -1 == _x)
return 0;
int i = _s->hasin('\n', _x);
if(-1 == i){
_x = -1;
if(COMM != (*_s)[_x] && _x != _s->len()-1){
Copy(*_s, _x, _s->len()-1, Line);
return Line;
}
return 0;
}
if(COMM != (*_s)[_x]){
Copy(*_s, _x, i, Line);
_x = i+1;
return Line;
}
else{
_x = i+1;
return get(Line);
}
}